package com.company.cloud.base.modules.mgr.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.base.modules.base.entity.ParameterInfo;
import com.company.cloud.base.modules.base.service.ParameterInfoService;
import com.company.cloud.base.modules.cache.models.ParameterModel;
import com.company.cloud.base.modules.cache.service.ParameterCacheService;
import com.company.cloud.base.modules.cache.utils.BaseConvertUtils;
import com.company.cloud.base.modules.mgr.controller.req.ParameterAddParam;
import com.company.cloud.base.modules.mgr.controller.req.ParameterUpdateParam;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Authc
@RestController
@RequestMapping("/mgr/base/parameter")
@Api(tags = "Mgr_Base_业务参数信息")
public class ParameterMgrController {

    @Autowired
    private ParameterInfoService parameterInfoService;

    @Autowired
    private ParameterCacheService parameterCacheService;

    @GetMapping("/get")
    @ApiOperation(value = "详情", response = ParameterModel.class)
    public BaseResult get(@RequestParam("id") Integer id) {
        if (CheckUtils.isEmpty(id)) {
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        ParameterInfo entity = parameterInfoService.getById(id);
        if (entity == null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        ParameterModel data = BaseConvertUtils.getParameterModel(entity);
        return BaseResult.ok(data);
    }

    @PostMapping("/save")
    @StaffLogc(title = "Mgr_Base_业务参数信息新增", action = "Mgr_Base_业务参数信息新增", actionAdo = "Mgr_Base_业务参数信息列表")
    @ApiOperation(value = "新增", response = BaseResult.class)
    public BaseResult save(@RequestBody ParameterAddParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        ParameterInfo entity = parameterInfoService.getOne(new QueryWrapper<ParameterInfo>()
                .eq("para_key", params.getParaKey())
                .eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
        );
        if (entity != null) {
            entity.setParaName(params.getParaName());
            entity.setParaVal(params.getParaVal());
            entity.setProductNo(params.getProductNo());
            entity.setParaRemark(params.getParaRemark());
            entity.setParaType(params.getParaType());
            entity.setOrders(params.getOrders());
            entity.addEditParam(userInfo);
            parameterInfoService.updateById(entity);
            parameterCacheService.removeParameterByKey2Cache(entity.getParaKey());
        } else {
            entity = new ParameterInfo();
            BeanUtil.copyProperties(params, entity);
            entity.addCreateParam(userInfo);
            entity.addEditParam(userInfo);
            parameterInfoService.save(entity);
            parameterCacheService.removeParameterByKey2Cache(entity.getParaKey());
        }
        return BaseResult.ok();
    }

    @PostMapping("/update")
    @StaffLogc(title = "Mgr_Base_业务参数信息修改", action = "Mgr_Base_业务参数信息修改", actionAdo = "Mgr_Base_业务参数信息列表")
    @ApiOperation(value = "修改", response = BaseResult.class)
    public BaseResult update(@RequestBody ParameterUpdateParam params) {
        ValidatorUtils.validate(params);
        ParameterInfo entity = parameterInfoService.getById(params.getId());
        if (entity == null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        entity.setParaName(params.getParaName());
        entity.setParaVal(params.getParaVal());
        entity.setParaRemark(params.getParaRemark());
        entity.setParaType(params.getParaType());
        entity.setOrders(params.getOrders());
        entity.addEditParam(userInfo);
        parameterInfoService.updateById(entity);
        parameterCacheService.removeParameterByKey2Cache(entity.getParaKey());
        return BaseResult.ok();
    }


}
